package com.myk.game.gobangchess.expand.bwantispace;

import com.myk.game.gobangchess.rooms.prerooms.BWAntiSpaceRoom;

import java.util.ArrayList;
import java.util.List;

/**
 * 游戏状态辅助方法的封装
 */
public class GameHelper {
    public final BWAntiSpaceRoom room;
    public final Game game;

    public GameHelper(BWAntiSpaceRoom room, Game game) {
        this.room = room;
        this.game = game;
    }

    /**
     * 判断指定位置是否为空格
     */
    public boolean isEmptyPos(int row, int col) {
        return room.curGrids[row][col] == 0;
    }

    /**
     * 获取一个随机的棋盘边缘空位置
     */
    public int[] getRandomBoardSideEmptyPosition() {
        return room.manager.getRandomBoardSideEmptyPosition();
    }

    /**
     * 获取场上所有棋子集合
     */
    public List<BasePiece> getAllFieldPieces() {
        List<BasePiece> allPieces = new ArrayList<>();
        if (game.fieldMonsterList != null) {
            allPieces.addAll(game.fieldMonsterList);
        }
        if (game.fieldPropList != null) {
            allPieces.addAll(game.fieldPropList);
        }
        if (game.human != null) {
            allPieces.add(game.human);
        }
        return allPieces;
    }

    /**
     * 获取场上所有怪物，并根据它们离主角的距离进行排序
     */
    public List<BaseMonsterPiece> getAllMonsterPiecesAndSort() {
        List<BaseMonsterPiece> monsterList = new ArrayList<>(game.fieldMonsterList);
        monsterList.sort((o1, o2) -> {
            int dis1 = Math.abs(o1.row - game.human.row) + Math.abs(o1.col - game.human.col);
            int dis2 = Math.abs(o2.row - game.human.row) + Math.abs(o2.col - game.human.col);
            return dis1 - dis2;
        });
        return monsterList;
    }

    /**
     * 获取指定位置的棋子 (有可能为空)
     */
    public BasePiece getPieceByPos(int row, int col) {
        for (BasePiece piece : getAllFieldPieces()) {
            if (piece.row == row && piece.col == col) {
                return piece;
            }
        }
        return null;
    }

    /**
     * 获取指定位置的怪物 (有可能为空)
     */
    public BaseMonsterPiece getMonsterByPos(int row, int col) {
        for (BaseMonsterPiece monster : game.fieldMonsterList) {
            if (monster.row == row && monster.col == col) {
                return monster;
            }
        }
        return null;
    }

    /**
     * 通知玩家操作
     */
    public void noticeHumanAction(String tipMsg) {
        room.manager.setGameTip(tipMsg);
        room.manager.noticeHumanMove();
    }

    /**
     * 通知ai机器人操作
     */
    public void noticeRobotAction() {
        game.liveDay += 1; //存活天数加1
        room.manager.noticeRobotMove();
    }

    /**
     * 显示棋子详细信息
     */
    public void showPieceInfo(IGameInformation piece) {
        room.manager.showInfoDialog(piece.getInformation());
    }

    /**
     * 获取棋盘行数
     */
    public int getRowCount() {
        return room.curGrids.length;
    }

    /**
     * 获取棋盘列数
     */
    public int getColCount() {
        return room.curGrids[0].length;
    }
}
